import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;


public class Polygon3D {
		List<Vertex3D> vertices;
		double xAxisTheta, yAxisTheta, zAxisTheta;
		int centerX, centerY;
		
		/*public Polygon() {
			this.vertices = new ArrayList<Vertex>();
		}*/
		
		public Polygon3D(int centerX, int centerY, Vertex3D... vertices) {
			this.vertices = new ArrayList<Vertex3D>();
			
			double maxX = 0, maxY = 0;
			for (Vertex3D v : vertices) {
				if (v.x > maxX)
					maxX = v.x;
				if (v.y > maxY)
					maxY = v.y;
			}
			for (Vertex3D v : vertices) {
				v.x -= maxX/2;
				v.y -= maxY/2;
				this.vertices.add(v);
			}
			
			xAxisTheta = 0;
			yAxisTheta = 0;
			zAxisTheta = 0;
			this.centerX = centerX;
			this.centerY = centerY;
		}
		
		public void addVertex(double x, double y) {
			addVertex(x, y, 0);
		}
		
		public void addVertex(double x, double y, double z) {
			vertices.add(new Vertex3D(x, y, z));
		}
		
		public void drawLine(BufferedImage b, int x1, int y1, int x2, int y2) {
			Graphics2D g = b.createGraphics();
			g.setColor(Color.black);
			
			int centerX = b.getWidth() / 2 + this.centerX;
			int centerY = b.getHeight() / 2 - this.centerY;
			
			g.drawLine(centerX + x1, centerY - y1, centerX + x2, centerY - y2);
		}
		
		public void drawTo(BufferedImage b) {
			//double projectedX1, projectedY1, projectedX2, projectedY2;
			Graphics2D g = b.createGraphics();
			g.setColor(Color.black);
			//int centerX = b.getWidth() / 2;
			//int centerY = b.getHeight() / 2;
			//xAxisTheta = Math.toRadians(30);
			//xAxisTheta = 30;
			
			List<Vertex3D> rotatedVertices = new ArrayList<Vertex3D>();
			for (Vertex3D v : vertices) {
				rotatedVertices.add(v.rotate(xAxisTheta, yAxisTheta, zAxisTheta));
			}
			
			double lastX, lastY;
			Vertex3D v = rotatedVertices.get(rotatedVertices.size() - 1);
			/*double[][] scaled = Part1.multiply(new double[][]{
					{v.z + 10, 0, 0},
					{0, v.z + 10, 0},
					{0, 0, v.z + 10}
				}, v.getMatrix());*/
			double[][] scaled = Part1.multiply(new double[][]{
					{10, 0, 0},
					{0, 10, 0},
					{0, 0, 10}
				}, v.getMatrix());
			lastX = scaled[0][0];
			lastY = scaled[1][0];
			for (int i = 0; i < rotatedVertices.size(); i++) {
				v = rotatedVertices.get(i);
				double newX, newY;
				/*scaled = Part1.multiply(new double[][]{
					{v.z + 10, 0, 0},
					{0, v.z + 10, 0},
					{0, 0, v.z + 10}
				}, v.getMatrix());*/
				scaled = Part1.multiply(new double[][]{
						{10, 0, 0},
						{0, 10, 0},
						{0, 0, 10}
					}, v.getMatrix());
				newX = scaled[0][0];
				newY = scaled[1][0];
				System.out.println(lastX + " " + lastY + " " + newX + " " + newY);
				drawLine(b, (int)lastX * 5, (int)lastY * 5, (int)newX * 5, (int)newY * 5);
				//drawLine(b, 0, 0, 10, 10);
				
				lastX = newX;
				lastY = newY;
			}
			
		}
	}
